# Create your views here.
from gmoney.members.forms import MemberForm, ChargingForm, ActivityForm
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.core.context_processors import csrf
from django.core.exceptions import ObjectDoesNotExist
from gmoney.members import service
from gmoney.members import models
from django.http import HttpResponse

def index(request):
    team_id = 1
    return render_to_response('index.html', {'team':models.Team.objects.get(id=team_id)})
    
def list_members(request):
    team_id = 1
    try:
        members = service.getMembers(team_id = 1)
    except ObjectDoesNotExist:
        return render_to_response('members_is_null.html')
    else:
        return render_to_response('list_members.html', {'members':members,})

def add_member(request):
    team_id = 1
    
    if request.method == 'POST':
        form = MemberForm(request.POST)
        if form.is_valid():
            nickname = form.cleaned_data['nickname']
            try:
                service.addMember(team_id = 1, nickname=nickname)
            except TeamNotExist:
                pass
            except MemberExist:
                pass
            return HttpResponseRedirect('/members/')
    else:
        form = MemberForm()
    
    c = {'form':form}
    c.update(csrf(request))
    return render_to_response('add_member.html', c)

def charging(request, member_id):
    team_id = 1
    
    if request.method == 'POST':
        form = ChargingForm(request.POST)
        if form.is_valid():
            money = form.cleaned_data['money']
            last_member_id = request.session.get('member_id', 0)
            if member_id != last_member_id:
                raise Http404
            try:
                service.addCharging(team_id = team_id, member_id = member_id, money = money)
            except ObjectDoesNotExist:
                pass
#            except MemberExist:
#                pass
            return HttpResponseRedirect('/members/')
    else:
        # session to save member_id
        request.session['member_id'] = member_id
        form = ChargingForm()
    
    c = {'form':form}
    c.update(csrf(request))
    return render_to_response('charging.html', c)

def list_activities(request):
    team_id=1
    try:
        activities = service.getActivities(team_id=team_id)
    except:
        pass
    return render_to_response('list_activities.html', {'activities':activities})
    
def add_activity(request):
    
    team_id = 1
    queryset = models.Team.objects.get(id=team_id).members.all()
    
    if request.method == 'POST':
        form = ActivityForm(queryset, request.POST)
        
        if form.is_valid():
            
            timestamp = form.cleaned_data['timestamp']
            place = form.cleaned_data['place']
            content = form.cleaned_data['content']
            money = form.cleaned_data['money']
            members = form.cleaned_data['members']
            #assert False
            service.addActivity(team_id=team_id, timestamp=timestamp, place=place, content=content, money=money, members=members)
            return HttpResponseRedirect('/activities/')
    else:
        form = ActivityForm(queryset)
    
    c = {'form':form}
    c.update(csrf(request))
    return render_to_response('add_activity.html', c)
    
